<?php 
class ControllerProductCategory extends Controller { 
	private $error = array();
 
	public function index() {
		$this->load->language('product/category');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/category');
		$this->getList();
	}

	public function insert() {
		$this->load->language('product/category');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/category');
		
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_category->addCategory($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/category', 'token=' . $this->session->data['token'], 'SSL')); 
		}

		$this->getFormText();
		$this->getInsertForm();
	}

	public function update() {
		$this->load->language('product/category');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/category');
		
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_category->editCategory($this->request->get['category_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/category', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->getFormText();
		$this->getUpdateForm();
	}
	
	public function updateSortOrder() {
		$this->load->language('product/category');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/category');
		
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_category->editCategorySortOrder($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/category', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->getList();
	}

	private function getList() {
   		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/category', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		$this->data['insert'] = $this->url->link('product/category/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['updateSortOrder'] = $this->url->link('product/category/updateSortOrder', 'token=' . $this->session->data['token'], 'SSL');
		
		$filter_category_id = 0;
		if (isset($this->request->get['filter_category_id'])) {
			$filter_category_id = $this->request->get['filter_category_id'];
		}
		$this->data['filter_category_id'] = $filter_category_id;
		
		$this->data['categories'] = array();
		$results = $this->model_product_category->getCategories($filter_category_id);
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/category/update', 'token=' . $this->session->data['token'] . '&category_id=' . $result['category_id'], 'SSL')
			);
			$action[] = array(
				'text' => $this->language->get('text_export'),
				'href' => $this->url->link('tool/export/download', 'token=' . $this->session->data['token'] . '&category_id=' . $result['category_id'], 'SSL')
			);
			$this->data['categories'][] = array(
				'category_id' => $result['category_id'],
				'name'        => $result['name'],
				'display_name'=> $result['display_name'],
				'sort_order'  => $result['sort_order'],
				'status'      => $result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
				'action'      => $action,
				'child'       => $this->getListChild($result['category_id'])
			);
		}
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_submit'] = $this->language->get('button_submit');		
		$this->data['button_filter'] = $this->language->get('button_filter');
 		$this->data['token'] = $this->session->data['token'];
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$this->template = 'product/category_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	private function getListChild($parent_id) {
		$results = $this->model_product_category->getCategoriesByPid($parent_id);
		$resultChild = array();
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/category/update', 'token=' . $this->session->data['token'] . '&category_id=' . $result['category_id'], 'SSL')
			);
			$action[] = array(
				'text' => $this->language->get('text_export'),
				'href' => $this->url->link('tool/export/download', 'token=' . $this->session->data['token'] . '&category_id=' . $result['category_id'], 'SSL')
			);
			$resultChild[] = array(
				'category_id' => $result['category_id'],
				'name'        => $result['name'],
				'display_name'=> $result['display_name'],
				'sort_order'  => $result['sort_order'],
				'status'      => $result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
				'action'      => $action,
				'child'       => $this->getListChild($result['category_id'])
			);
		}
		return $resultChild;
	}

	private function getFormText() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');		
		$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_percent'] = $this->language->get('text_percent');
		$this->data['text_amount'] = $this->language->get('text_amount');
				
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_display_name'] = $this->language->get('entry_display_name');
		$this->data['entry_meta_keyword'] = $this->language->get('entry_meta_keyword');
		$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_keyword'] = $this->language->get('entry_keyword');
		$this->data['entry_parent'] = $this->language->get('entry_parent');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

    	$this->data['tab_general'] = $this->language->get('tab_general');
    	$this->data['tab_data'] = $this->language->get('tab_data');
		$this->data['tab_design'] = $this->language->get('tab_design');
		
		$this->data['error_warning'] = '';
		$this->data['error_name'] = array();

		$this->data['token'] = $this->session->data['token'];
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/category', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
	}
	
	private function getInsertForm() {
		$this->data['action'] = $this->url->link('product/category/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/category', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		$this->data['category_description'] = array();
		$this->data['categories'] = $this->model_product_category->getAllCategoriesForSelect(0, '', '', -1);
		$this->data['parent_id'] = 0;
		
		$this->data['name'] = '';
		$this->data['image'] = '';
		$this->load->model('tool/image');
		$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		$this->data['sort_order'] = 0;
		$this->data['status'] = 1;
		$this->data['layout_id'] = 0;
		
		$this->load->model('design/layout');
		$this->data['layouts'] = $this->model_design_layout->getLayouts();
						
		$this->template = 'product/category_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	private function getUpdateForm() {
		$this->data['action'] = $this->url->link('product/category/update', 'token=' . $this->session->data['token'] . '&category_id=' . $this->request->get['category_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/category', 'token=' . $this->session->data['token'], 'SSL');

		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		$this->data['categories'] = $this->model_product_category->getAllCategoriesForSelect(0, '', '', -1);
		
		$category_info = $this->model_product_category->getCategory($this->request->get['category_id']);
		$this->data['category_description'] = $this->model_product_category->getCategoryDescriptions($this->request->get['category_id']);
		
		$this->data['parent_id'] = $category_info['parent_id'];
		$this->data['name'] = $category_info['name'];
		$this->data['image'] = $category_info['image'];
		$this->load->model('tool/image');
		if ($category_info['image'] && file_exists(DIR_IMAGE . $category_info['image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($category_info['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		$this->data['sort_order'] = $category_info['sort_order'];
		$this->data['status'] = $category_info['status'];
		$this->data['layout_id'] = $category_info['layout_id'];
		
		$this->load->model('design/layout');
		
		$this->data['layouts'] = $this->model_design_layout->getLayouts();
						
		$this->template = 'product/category_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
}
?>